package com.aj.activity.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.GridView;
import android.widget.ImageView;

import com.aj.activity.R;
import com.aj.utils.PortraitLoaderUtil;

/**
 * The Class MailGroupContactsAdapter.
 */
public class ChoosePhotoAdapter extends BaseAdapter {
	/** The Constant TAG. */
	private final static String TAG = ChoosePhotoAdapter.class.getName();

	/** The context. */
	private static Context context;

	/** The Constant PHOTO_FILETYPE. */
	private static final int PHOTO_FILETYPE = 0;

	/** The inflater. */
	private final LayoutInflater inflater;

	/** The contacts list. */
	private final List<String> filePathList = new ArrayList<String>();
	private final List<String> choosePathList = new ArrayList<String>();
	/** The file type. */
	private int fileType = PHOTO_FILETYPE;

	/** The defualt bitmap. */
	private static Bitmap defualtBitmap = null;

	/**
	 * Instantiates a new select fax adapter.
	 * 
	 * @param mGridView
	 *            the m grid view
	 * @param context
	 *            the context
	 * @param fileList
	 *            the file list
	 * @param fileType
	 *            the file type
	 */
	public ChoosePhotoAdapter(GridView mGridView, Context context,
			List<String> fileList, int fileType) {
		filePathList.clear();
		choosePathList.clear();
		filePathList.addAll(fileList);
		this.inflater = LayoutInflater.from(context);
		this.fileType = fileType;
		this.context = context;
	}

	/**
	 * Gets the count.
	 * 
	 * @return the count
	 */
	@Override
	public int getCount() {
		return filePathList.size();
	}

	/**
	 * Gets the item.
	 * 
	 * @param position
	 *            the position
	 * @return the item
	 */
	@Override
	public Object getItem(int position) {
		return filePathList.get(position);
	}

	/**
	 * Gets the item id.
	 * 
	 * @param position
	 *            the position
	 * @return the item id
	 */
	@Override
	public long getItemId(int position) {
		return position;
	}

	/**
	 * The Class FileHodler.
	 */
	public class FileHodler {
		/** The phone tv. */
		ImageView imagegrid;

		/** The vedioicon. */
		ImageView vedioicon;

		CheckBox checkbox_image;

		/** The index. */
		int index;

		/** The is download photo. */
		boolean isDownloadPhoto = false;

		/**
		 * Checks if is download photo.
		 * 
		 * @return true, if is download photo
		 */
		public boolean isDownloadPhoto() {
			return isDownloadPhoto;
		}

		/**
		 * Sets the download photo.
		 * 
		 * @param isDownloadPhoto
		 *            the new download photo
		 */
		public void setDownloadPhoto(boolean isDownloadPhoto) {
			this.isDownloadPhoto = isDownloadPhoto;
		}

		/**
		 * Gets the imagegrid.
		 * 
		 * @return the imagegrid
		 */
		public ImageView getImagegrid() {
			return imagegrid;
		}

		/**
		 * Sets the imagegrid.
		 * 
		 * @param imagegrid
		 *            the new imagegrid
		 */
		public void setImagegrid(ImageView imagegrid) {
			this.imagegrid = imagegrid;
		}

		/**
		 * Gets the index.
		 * 
		 * @return the index
		 */
		public int getIndex() {
			return index;
		}

		/**
		 * Sets the index.
		 * 
		 * @param index
		 *            the new index
		 */
		public void setIndex(int index) {
			this.index = index;
		}

		public ImageView getVedioicon() {
			return vedioicon;
		}

		public void setVedioicon(ImageView vedioicon) {
			this.vedioicon = vedioicon;
		}

		public CheckBox getCheckbox_image() {
			return checkbox_image;
		}

		public void setCheckbox_image(CheckBox checkbox_image) {
			this.checkbox_image = checkbox_image;
		}

		/**
		 * Hash code.
		 * 
		 * @return the int
		 * @version
		 */
		@Override
		public int hashCode() {
			return index == 0 ? super.hashCode() : index;
		}
	}

	/**
	 * Gets the view.
	 * 
	 * @param position
	 *            the position
	 * @param convertView
	 *            the convert view
	 * @param parent
	 *            the parent
	 * @return the view
	 */
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		FileHodler fileHodler = new FileHodler();
		convertView = inflater.inflate(R.layout.item_choosefile, null);
		fileHodler.imagegrid = (ImageView) convertView
				.findViewById(R.id.imagegrid);
		fileHodler.vedioicon = (ImageView) convertView
				.findViewById(R.id.vedioicon);
		fileHodler.imagegrid.setTag("" + position);
		fileHodler.index = position;
		fileHodler.checkbox_image = (CheckBox) convertView
				.findViewById(R.id.checkbox_image);
		final String filePath = filePathList.get(position);
		fileHodler.checkbox_image
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if (isChecked) {
							if (choosePathList.size() >= 5) {
								buttonView.setChecked(false);
								return;
							}
							choosePathList.add(filePath);
						} else {
							choosePathList.remove(filePath);
						}
					}
				});
		convertView.setTag(fileHodler);
		View mainview = convertView;
		if (position == 0 || fileType == PHOTO_FILETYPE) {
			fileHodler.vedioicon.setVisibility(View.INVISIBLE);
		} else {
			fileHodler.vedioicon.setVisibility(View.VISIBLE);
		}

		PortraitLoaderUtil.loadSrc(context, fileHodler.imagegrid, filePath,
				null, R.drawable.photo_gridview_backgourd,
				R.drawable.photo_gridview_backgourd);
		return mainview;
	}

	public List<String> getChoosePathList() {
		return choosePathList;
	}

}
